package com.byq.mm.smartlib.tools;



import java.util.Map;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.byq.mm.smartlib.activity.AddFriendRequestActivity;

import com.byq.mm.smartlib.activity.ConversationList;
import com.byq.mm.smartlib.activity.OfferActivity;
import com.byq.mm.smartlib.R;
import com.byq.mm.smartlib.activity.RequestActivity;
import com.byq.mm.smartlib.SmartActionConstants;
import com.byq.mm.abstractx.protocol.Defination;
import com.byq.mm.smartlib.activity.Constants;
import com.byq.mm.smartlib.engine.NotificationHandler;


public class NotificationHandlerImpl implements NotificationHandler {
	public NotificationHandlerImpl(Context ctx) {
		super();
		this.ctx = ctx;
	}

	Context ctx;
	public void updateService(Context ctx, int state) {
		int rid = 0;
		int sid = 0;
		switch(state){
		case 0:
		{
			rid = R.drawable.offline;
			sid = R.string.offline;
			break;
		}
		case 1:
		{
			rid = R.drawable.connnecting;
			sid = R.string.connecting;
			break;
		}
		case 2:
		{
			rid = R.drawable.online;
			sid = R.string.online;
		}
		}
		
		RemoteViews contentView = new RemoteViews(ctx.getPackageName(),
				R.layout.smart_notification);
		contentView.setImageViewResource(R.id.presence, rid);
		String s = ctx.getResources().getString(sid);
		contentView.setTextViewText(R.id.text, s);
		
		
		CharSequence appName = ctx.getResources().getString(R.string.app_name);



		Notification.Builder builder = new Notification.Builder(ctx);

		PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
				new Intent("com.byq.mm.action.START_MAIN_APP").setPackage(ctx.getPackageName()), PendingIntent.FLAG_IMMUTABLE|PendingIntent.FLAG_UPDATE_CURRENT);


		builder.setSmallIcon(R.drawable. smart_app)
				.setContentTitle(appName)
				.setContentIntent(contentIntent);
		Notification notification = builder.getNotification();

		
		
		if (rid == R.drawable.connnecting) {
			notification.flags |= Notification.FLAG_SHOW_LIGHTS;
			notification.ledARGB = 0xffff0000; /* red */
			notification.ledOnMS = 125;
			notification.ledOffMS = 2875;
		}

		notification.flags |= Notification.FLAG_ONGOING_EVENT;


//		ctx.startForeground(R.string.remoteservicestarted, notification);
		notification.contentView = contentView;
		NotificationManager nm = (NotificationManager) ctx
		.getSystemService(Context.NOTIFICATION_SERVICE);
		if(state == 2)
			notification.defaults = Notification.DEFAULT_ALL;
		nm.notify(R.layout.smart_notification, notification);
	
	}
	static public void startService(Service ctx, int state) {
		int rid = 0;
		int sid = 0;
		switch(state){
		case 0:
		{
			rid = R.drawable.offline;
			sid = R.string.offline;
			break;
		}
		case 1:
		{
			rid = R.drawable.connnecting;
			sid = R.string.connecting;
			break;
		}
		case 2:
		{
			rid = R.drawable.online;
			sid = R.string.online;
		}
		}
		
		RemoteViews contentView = new RemoteViews(ctx.getPackageName(),
				R.layout.smart_notification);
		contentView.setImageViewResource(R.id.presence, rid);
		String s = ctx.getResources().getString(sid);
		contentView.setTextViewText(R.id.text, s);
		
		
		CharSequence appName = ctx.getResources().getString(R.string.app_name);

		Notification.Builder builder = new Notification.Builder(ctx);

		PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
				new Intent("com.byq.mm.action.START_MAIN_APP").setPackage(ctx.getPackageName()), PendingIntent.FLAG_IMMUTABLE|PendingIntent.FLAG_UPDATE_CURRENT);


		builder.setSmallIcon(R.drawable. smart_app)
				.setContentTitle(appName)
				.setContentIntent(contentIntent);
		Notification notification = builder.getNotification();
		
		
		if (rid == R.drawable.connnecting) {
			notification.flags |= Notification.FLAG_SHOW_LIGHTS;
			notification.ledARGB = 0xffff0000; /* red */
			notification.ledOnMS = 125;
			notification.ledOffMS = 2875;
		}

		notification.flags |= Notification.FLAG_ONGOING_EVENT;
	


//		ctx.startForeground(R.string.remoteservicestarted, notification);
		notification.contentView = contentView;
//		NotificationManager nm = (NotificationManager) ctx
//		.getSystemService(Context.NOTIFICATION_SERVICE);
		if(state == 2)
			notification.defaults = Notification.DEFAULT_ALL;
		//nm.notify(R.layout.smart_notification, notification);
		//ctx.startForeground(R.layout.smart_notification, notification);
	}

	static public void removeSevice(Context ctx) {
		NotificationManager mNM = (NotificationManager) ctx
				.getSystemService(Context.NOTIFICATION_SERVICE);

		mNM.cancel(R.layout.smart_notification);

	}
	public void notifyInvite(Defination de) {
		NotificationManager nm = (NotificationManager) ctx
				.getSystemService(ctx.NOTIFICATION_SERVICE);
		Notification n = new Notification(R.drawable.device, de.getDevice()
				.getId()
				+ ctx.getResources().getString(R.string.notify_invite), System
				.currentTimeMillis());

		Intent i = new Intent(ctx, OfferActivity.class);
		i.setPackage(ctx.getPackageName());
		i.putExtra("defination", de);
		
		PendingIntent pi = null;
		try {
			pi = PendingIntent.getActivity(ctx, 0, i,
					PendingIntent.FLAG_IMMUTABLE|PendingIntent.FLAG_UPDATE_CURRENT);
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		n.flags |= Notification.FLAG_SHOW_LIGHTS;
		n.flags |= Notification.FLAG_AUTO_CANCEL;
		n.flags |= Notification.DEFAULT_SOUND;
		n.defaults = Notification.DEFAULT_ALL;
		nm.notify(de.hashCode(), n);
	}

	public void notifyAccept(String jid) {
		NotificationManager nm = (NotificationManager) ctx
				.getSystemService(ctx.NOTIFICATION_SERVICE);

		Notification n = new Notification(R.drawable.device, jid
				+ ctx.getResources().getString(R.string.notify_accept), System
				.currentTimeMillis());

		Intent i = new Intent("dummy");
		i.setPackage(ctx.getPackageName());
		PendingIntent pi = null;
		try {
			pi = PendingIntent.getActivity(ctx, 0, i,
					PendingIntent.FLAG_IMMUTABLE|PendingIntent.FLAG_UPDATE_CURRENT);
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//n.setLatestEventInfo(ctx, ctx.getResources().getString(
			//	R.string.device_desc), jid
			//	+ ctx.getResources().getString(R.string.notify_accept), pi);
		n.flags |= Notification.FLAG_SHOW_LIGHTS;
		n.flags |= Notification.FLAG_AUTO_CANCEL;
		n.flags |= Notification.DEFAULT_SOUND;
		n.defaults = Notification.DEFAULT_ALL;
		nm.notify(jid.hashCode(), n);
		
	}

	public void notifyRefuse(String jid) {
		NotificationManager nm = (NotificationManager) ctx
				.getSystemService(ctx.NOTIFICATION_SERVICE);
		Notification n = new Notification(R.drawable.device, jid
				+ ctx.getResources().getString(R.string.refuse), System
				.currentTimeMillis());
		Intent i = new Intent("dummy");
		i.setPackage(ctx.getPackageName());
		PendingIntent pi = null;
		try {
			pi = PendingIntent.getActivity(ctx, 0, i,
					PendingIntent.FLAG_IMMUTABLE|PendingIntent.FLAG_UPDATE_CURRENT);
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//n.setLatestEventInfo(ctx, ctx.getResources().getString(
		//		R.string.device_desc), jid
		//		+ ctx.getResources().getString(R.string.refuse), pi);
		n.flags |= Notification.FLAG_SHOW_LIGHTS;
		n.flags |= Notification.FLAG_AUTO_CANCEL;
		n.flags |= Notification.DEFAULT_SOUND;
		n.defaults = Notification.DEFAULT_ALL;
		nm.notify(jid.hashCode(), n);
	}

	public void notifyUser(Map<String, String> msg) {
		NotificationManager nm = (NotificationManager) ctx
				.getSystemService(ctx.NOTIFICATION_SERVICE);
		Notification n = new Notification(R.drawable.newmail_22, msg.get("body"), System
				.currentTimeMillis());

		Intent i = new Intent(ctx, ConversationList.class);
		i.setPackage(ctx.getPackageName());
		i.putExtra("newReceived", true);
		PendingIntent pi = PendingIntent.getActivity(ctx, 0, i, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
		//n.setLatestEventInfo(ctx, "You received a new Message", "From: "
		//		+ msg.get("user"), pi);
		n.defaults = Notification.DEFAULT_ALL;
		nm.notify(Constants.NEW_MESSAGE_NOTIFICATION, n);
		
		Intent intent = new Intent(SmartActionConstants.NEW_MESSAGE);
		intent.setPackage(ctx.getPackageName());
		intent.putExtra("test", msg.get("body"));
		ctx.sendBroadcast(intent);
	}

	

	@Override
	public void notifyRequest(Defination de, String from) {
		NotificationManager nm = (NotificationManager) ctx
				.getSystemService(ctx.NOTIFICATION_SERVICE);
		Notification n = new Notification(R.drawable.device,from
				+ ctx.getResources().getString(R.string.notify_request), System
				.currentTimeMillis());
		
		Intent i = new Intent(ctx, RequestActivity.class);
		i.setPackage(ctx.getPackageName());
		i.putExtra("defination", de);
		i.putExtra("from", from);
		PendingIntent pi = null;
		try {
			pi = PendingIntent.getActivity(ctx, 0, i,
					PendingIntent.FLAG_IMMUTABLE|PendingIntent.FLAG_UPDATE_CURRENT);
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//n.setLatestEventInfo(ctx, ctx.getResources().getString(
		//		R.string.device_desc), from
		//		+ ctx.getResources().getString(R.string.notify_request), pi);
		n.flags |= Notification.FLAG_SHOW_LIGHTS;
		n.flags |= Notification.FLAG_AUTO_CANCEL;
		n.flags |= Notification.DEFAULT_SOUND;
		n.defaults = Notification.DEFAULT_ALL;
		nm.notify(de.hashCode(), n);
	}
	@Override
	public void notifyRequestRefuse(Defination de, String from) {
		notifySimply(de,from,R.string.notify_request_auto_refused, null);
	}
	@Override
	public void notifyRequestAgree(Defination de, String from) {
		notifySimply(de,from,R.string.notify_request_auto_agreed, null);
	}
	@Override
	public void notifyOfferAccepted(Defination de, String from) {
		notifySimply(de,from,R.string.notify_offer_auto_accepted, null);
	}
	@Override
	public void notifyOfferRejected(Defination de, String from) {
		notifySimply(de,from,R.string.notify_offer_auto_rejected, null);
	}
	public void notifySimply(Defination de, String from, int rid, Intent intent) {
		String info = ctx.getResources().getString(rid);
		NotificationManager nm = (NotificationManager) ctx
				.getSystemService(ctx.NOTIFICATION_SERVICE);
		Notification n = new Notification(R.drawable.device,from
				+ info, System
				.currentTimeMillis());
		if(intent == null)
			intent = new Intent("dummy");
		intent.setPackage(ctx.getPackageName());
		PendingIntent pi = null;
		try {
			pi = PendingIntent.getActivity(ctx, 0, intent,
					PendingIntent.FLAG_IMMUTABLE|PendingIntent.FLAG_UPDATE_CURRENT);
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//n.setLatestEventInfo(ctx, ctx.getResources().getString(
		//		R.string.device_desc), from
			//	+ info, pi);
		n.flags |= Notification.FLAG_SHOW_LIGHTS;
		n.flags |= Notification.FLAG_AUTO_CANCEL;
		n.flags |= Notification.DEFAULT_SOUND;
		n.defaults = Notification.DEFAULT_ALL;
		nm.notify(de.hashCode(), n);
	}
	@Override
	public void notifyTransportClosed() {
//		Looper.prepare();
		Toast.makeText(ctx, R.string.transport_unavailable, Toast.LENGTH_SHORT).show();
		
	}

	@Override
	public void notifyUnavailable(String to) {
		String msg = to + ctx.getResources().getString(R.string.target_unavailable);
//		Looper.prepare();
		
		Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
		
	}
	
	@Override
	public void notifyAddContactRequest(String jid) {
		Intent i = new Intent(ctx, AddFriendRequestActivity.class);
		i.setPackage(ctx.getPackageName());
		i.putExtra("userName", jid);
		String info = ctx.getResources().getString(R.string.want_to_be_a_friend);
		NotificationManager nm = (NotificationManager) ctx
				.getSystemService(ctx.NOTIFICATION_SERVICE);
		Notification n = new Notification(R.drawable.contactadd_blank,jid
				+ " " + info, System
				.currentTimeMillis());
		
		PendingIntent pi = null;
		try {
			pi = PendingIntent.getActivity(ctx, 0, i,
					PendingIntent.FLAG_IMMUTABLE|PendingIntent.FLAG_UPDATE_CURRENT);
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	//	n.setLatestEventInfo(ctx, ctx.getResources().getString(
	//			R.string.add_friend_request), jid + " "
	//			+ info, pi);
		n.flags |= Notification.FLAG_SHOW_LIGHTS;
		n.flags |= Notification.FLAG_AUTO_CANCEL;
		n.flags |= Notification.DEFAULT_SOUND;
		n.defaults = Notification.DEFAULT_ALL;
		nm.notify(jid.hashCode(), n);
		
	}
	@Override
	public void notifyAddContactResonse(String jid, String type) {
		// TODO Auto-generated method stub
		
	}



}
